package com.jbeer.framework.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jbeer.framework.JBeerWeb;
import com.jbeer.framework.exception.RenderingViewException;
import com.jbeer.framework.web.ModelAndView.ViewType;
import com.jbeer.framework.web.viewrender.AJAXRender;
import com.jbeer.framework.web.viewrender.FreeMarkerRender;
import com.jbeer.framework.web.viewrender.JSONPRender;
import com.jbeer.framework.web.viewrender.JSONRender;
import com.jbeer.framework.web.viewrender.JSTLRender;
import com.jbeer.framework.web.viewrender.VelocityRender;

public class RenderingView {

    public static void rendering(HttpServletRequest request, HttpServletResponse response,
                                 ModelAndView modelView) throws RenderingViewException {
    	if(modelView==null){
    		return ;
    	}
        ViewType type = modelView.getViewType();
        try {
            switch (type) {
                case PAGE:
                    if (JBeerWeb.viewRender.equalsIgnoreCase(Render.JSTL.getName())) {
                        JSTLRender.render(request, response, modelView);
                    } else if (JBeerWeb.viewRender.equalsIgnoreCase(Render.FREEMARKER.getName())) {
                        FreeMarkerRender.render(request, response, modelView);
                    } else if (JBeerWeb.viewRender.equalsIgnoreCase(Render.VELOCITY.getName())) {
                        VelocityRender.render(request, response, modelView);
                    } else {
                        throw new RenderingViewException("Not support " + JBeerWeb.viewRender
                                                         + " type for view rendering");
                    }
                    return;
                case JSON:
                    JSONRender.render(request, response, modelView);
                    return;
                case JSONP:
                    JSONPRender.render(request, response, modelView);
                    return;
                case AJAX:
                    AJAXRender.render(request, response, modelView);
                    return;
                default:
                    break;
            }
        } catch (Exception e) {
            throw new RenderingViewException(e);
        }
    }

    static enum Render {

        JSTL("jstl"), FREEMARKER("freemarker"), VELOCITY("velocity");
        private String name;

        private Render(String name) {
            this.name = name;
        }

        String getName() {
            return name;
        }
    }
}
